package com.gravata.enigma.importer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.gravata.enigma.model.Transponder;

public class Sat{

	private final String SATMOLDE="\t<sat name=\"%s\" flags=\"%s\" position=\"%s\">";
	private final String TRANSMODEL="\t\t\t\t<transponder frequency=\"%s\" symbol_rate=\"%s\" polarization=\"%s\" fec_inner=\"%s\" system=\"%s\" modulation=\"%s\"/>";
	private int id;
	private	String nome;
	private	String strPosicao;
	private Map<String, Map<String, List<Transponder>>> transponderBanda=new HashMap<String, Map<String,List<Transponder>>>();
	private List<Transponder> transponders=new ArrayList<Transponder>();
	private String strSat;
	private int posicao;

	public Sat() {
		// TODO Auto-generated constructor stub
	}

	public int getId() {
		if(posicao>1800){
			id= Float.valueOf(((posicao)/10-360)*10).intValue();
		}
	    else{
	    	id= Float.valueOf(((posicao)/10+180)*10).intValue();
	    }
		return id;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}
	public String getNome() {
		return nome;
	}
	public void setStrPosicao(String strPosicao) {
		this.strPosicao = strPosicao;
	}
	public String getStrPosicao() {
		return strPosicao;
	}
	public void setPosicao(int posicao) {
		this.posicao = posicao;
	}

	public List<Transponder> getTransponders() {
		return transponders;
	}
	public void setTransponders(List<Transponder> transponders) {
		this.transponders = transponders;
	}

	public Map<String, Map<String, List<Transponder>>> getTransponderBanda() {
		return transponderBanda;
	}
	public void setTransponderBanda(
			Map<String, Map<String, List<Transponder>>> transponderBanda) {
		this.transponderBanda = transponderBanda;
	}


	public int getPosicao() {
		return posicao;
	}


	public void setStrSat(String strSat) {
		this.strSat = strSat;
	}
	public String getStrSat() {
		//<sat name="Hispasat 1D/1E (30.0W)" flags="0" position="-300">
		//<transponder frequency="10730000" symbol_rate="27500000" polarization="0" fec_inner="3" system="1" modulation="2"/>

		if(strSat==null){
			StringBuilder tmp=new StringBuilder();
			tmp.append(String.format(SATMOLDE, getNome(),"0",Integer.toString( getId()) ) ).append("\n");
			Set<String> bandas=getTransponderBanda().keySet();
			for(String banda:bandas){
				tmp.append("\t\t<!--"+banda+"-->\n");

				Map<String, List<Transponder>> tbanda=getTransponderBanda().get(banda);

				Set<String> paises=tbanda.keySet();

				for(String pais:paises){
					List<Transponder> transponders=tbanda.get(pais);

					tmp.append("\t\t\t<!--"+pais+"-->\n");

					for(Transponder transponder:transponders){
						tmp.append(String.format(TRANSMODEL,Integer.toString(transponder.getFrequency()),
								Integer.toString(transponder.getSymbolRate()),
								Integer.toString(transponder.getPolarization().ordinal()),
								Integer.toString(transponder.getFec().ordinal()),
								Integer.toString(transponder.getSystem().ordinal()),
								Integer.toString(transponder.getModulation().getNumero()))).append("\n");

					}
				}
			}
			tmp.append("\t</sat>");
			strSat=tmp.toString();
		}
		return strSat;
	}
}